import re

class SpecialLabel:
    def __init__(self, prefix, newLabelName, sortName ="quantity"):
        self.name =prefix
        self.newLabelName = newLabelName
        self.sortName = sortName
        self.labelBlockArray = []


class PutIn2Array:
    def __init__(self, stringFileAbsPath, prefixArray):
        self.stringFileAbsPath = stringFileAbsPath
        self.prefixArray = prefixArray

    def put(self):
        newFileLineArray =[]
        newFileLineArray.append('<?xml version="1.0" encoding="utf-8"?>\n<resources>\n')
        file = open(self.stringFileAbsPath, 'r')
        file_content = file.read()
        file_content = file_content[file_content.index("<resources>") + len("<resources>"):]
        file.close()

        regex =re.compile(r'<([a-zA-Z-]+)([^>]+)?>')
        next_file_index=0

        search_result = regex.search(file_content[next_file_index:] )
        while search_result:
            labelName = search_result.group(1)
            # <plurals[\s\S]+?</plurals>
            block_regex = re.compile( r'<'+ labelName+'[\s\S]+?</'+labelName+'>')
            block_search_result = block_regex.search(file_content[next_file_index:])

            labelBlock = block_search_result.group()
            labelBlockSpecial = False
            if "string" == labelName:
                string_regex = re.compile(r'<string\s+name\s*=\s*"([^"]+)"\s*>([\s\S]+?)</\s*string\s*>')
                string_regex_result = string_regex.search(labelBlock)
                if not string_regex_result:
                    continue
                else:
                    key = string_regex_result.group(1)
                    value = string_regex_result.group(2)
                    for prefix in self.prefixArray:
                        try :
                            if  key.index( prefix.name ) !=0 :
                                continue
                        except ValueError :
                            continue
                        # 该 key 为 special key
                        prefix.labelBlockArray.append(value)
                        labelBlockSpecial =True
                        break

            if not labelBlockSpecial:
                newFileLineArray.append(labelBlock)
            next_file_index += block_search_result.span()[1]

            search_result = regex.search(file_content[next_file_index:] )

        for item in self.prefixArray:
            text = "<" + item.newLabelName + " name=\"" + item.name+"\">"
            for item2 in item.labelBlockArray:
                if '' == item.sortName:
                    text += '<item>' + item2 +"</item>"
                else:
                    text += '<item ' + item.sortName + '="">'+ item2 +"</item>"
            text += "</"+item.newLabelName +">"
            newFileLineArray.insert(1, text)
        newFileLineArray.append("</resources>")

        file =open (self.stringFileAbsPath, 'w')
        for item in newFileLineArray:
            file.write(item)
        file.close()
if __name__ == '__main__':
    prefixArray = []
    prefixArray.append( SpecialLabel("num_product", "plurals"))
    prefixArray.append( SpecialLabel("delete_or_cancel", "string-array", ""))
    prefixArray.append( SpecialLabel("main_tab_titles", "string-array", ""))
    handler = PutIn2Array("/Users/h/AndroidStudioProjects/patpat/Patpat/src/main/res/values-el/strings.xml",prefixArray)
    handler.put()
